import Vue from 'vue'
import Router from 'vue-router'
import Store from '@/store'
import { getCookie } from '../tool/cookie'
function asyncImport(item) {
  return () => import(`@/pages/${item}`)
}

Vue.use(Router)
 const router = new Router({
  
  routes: [
    {
      path:'/login',
      name:'login',
      component: asyncImport('login'),
    },
    {
      path:'/',
      name:'home',
      component: asyncImport('home'),
      redirect:'/commodity',
      children:[
        // 轮播图管理
        // {
        //   path:'/broadCast',
        //   name:'broadCast',
        //   component:asyncImport('broadCast')
        // },
        // 商品管理
        {
          path:'/commodity',
          name:'commodity',
          component:asyncImport("commodity/demo")
        },
        {
          path:'/activity',
          name:'activity',
          component:asyncImport('activity/activity')
        },
        {
          path:'/order',
          name:'order',
          component:asyncImport('order')
        },
        {
          path:'/carriage',
          name:'carriage',
          component:asyncImport('carriage')
        },
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  // 获取cookie的identity值，如果存在就直接next()不存在的话就push到login
  let identity = getCookie('identity')
  if(identity && sessionStorage.getItem('identity') ){
    next()
  }else{
    if(to.name == 'login'){
      next()
    }else if(to.name != 'login'){
      next('/login')
    }
  }
  // // 判断是否已经登录
  // if(sessionStorage.token){
  //   // 如果token已经存在还在返回登录页那就禁止返回,不是的话就直接next
  //   if(to.name == 'login'){
  //     next(from.path)
  //   }else{
  //     next()
  //   }
  // }else{
  //   if(to.name == 'login'){
  //     next()
  //   }else if(to.name != 'login'){
  //     next('/login')
  //   }
  // }
})
export default router
